//
//  LYWBMainViewController.swift
//  智微博
//
//  Created by mac1 on 2017/4/17.
//  Copyright © 2017年 Y&Q. All rights reserved.
//

import UIKit

/// 主控制器
class LYWBMainViewController: UITabBarController {
  
  //设置页面旋转方向
  private var _orientations = UIInterfaceOrientationMask.portrait

    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildControllers()
        setupComposeButton()
    }
  
  //废弃的方法
//  /Users/mac1/Desktop/iOS_Code/swift3.0demo/weibo/智微博/智微博/Classes/View(视图和控制器)/Main/LYWBMainViewController.swift:20:9: Method 'supportedInterfaceOrientations()' with Objective-C selector 'supportedInterfaceOrientations' conflicts with getter for 'supportedInterfaceOrientations' from superclass 'UIViewController' with the same Objective-C selector
//  override func supportedInterfaceOrientations()->UIInterfaceOrientationMask {
//    
//  }
  
  //用此替代
  // 设置支持的方向之后,当前控制器及其子控制器都会遵守这个方向
  // 如果播放视频 是通过 modal 展现的
  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get { return self._orientations }
    set { self._orientations = newValue }
  }
  
  
  //MARK: - 监听方法
  ///撰写微博
  //FIXME:没有实现
  // private 能够保证方法私有, 仅在当前对象被访问
  // @objc 允许函数在运行时通过 OC 消息机制被调用!
 func composeStatus() {
    print("撰写微博")
  
  //测试方向旋转
  let vc = UIViewController()
  
  vc.view.backgroundColor = UIColor.cz_random()
  let nav = UINavigationController(rootViewController: vc)
  present(nav, animated: true, completion: nil)
  }
  
  //MARK: - 私有控件
  /// 撰写按钮
   lazy var composeButton:UIButton = UIButton.cz_imageButton(
      "tabbar_compose_icon_add",
      backgroundImageName: "tabbar_compose_button")
}

// extension 类似于 OC 中的分类,在 swift 中还可以用来切分代码块
// 可以把详尽功能的函数,放在一个 extension 中
// 注意: 和 OC 的分类一样,extension 中不能定义属性,只能定义方法
extension LYWBMainViewController {
  
  /// 设置撰写按钮
   func setupComposeButton() {
      tabBar.addSubview(composeButton)
    
    //设置按钮的位置
    //计算按钮的宽度
    let count = CGFloat(childViewControllers.count)
    
    //将向内缩进的宽度减少, 能够让按钮的宽度变大, 盖住容错点, 防止穿帮
    let w = tabBar.bounds.width / count - 1
    
    
    // CGRectInset 正数向内缩进, 负数向外扩展
    
    composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
    print("按钮宽度 \(composeButton.frame.width)")
    
    // 按钮监听方法
    composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    //composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    
  }
  
  
  /// 设置所有子控制器
    func setupChildControllers() {
    
    let array = [
      ["clsName":"LYWBHomeViewController","title":"首页","imageName":"home" ],
      ["clsName":"LYWBMessageViewController","title":"消息","imageName":"message_center" ],
      ["clsName":"UIViewController"],
      ["clsName":"LYWBDiscoverViewController","title":"发现","imageName":"discover" ],
      ["clsName":"LYWBProfileViewController","title":"我","imageName":"profile" ],
    ]
    
    var arrayM = [UIViewController]()
    
    for dict in array {
      
      arrayM.append(controller(dict: dict))
    
    }
    
    viewControllers = arrayM
    
  }
  
  
  /// 使用字典创建一个自控制器
  /// - parameter dict: 信息字典
  /// - return: 子控制器
  private func controller(dict:[String:String]) -> UIViewController {
    
    // 1. 取得字典内容
    guard let clsName = dict["clsName"],
      let title = dict["title"],
      let imageName = dict["imageName"],
      let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type else{
        return UIViewController()
    }
    
    // 2. 创建视图控制器
    // 1> 将 clsName 转换成 cls
    let vc = cls.init()
    
    vc.title = title
    
    // 3. 设置图像
    
    vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
    vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
    
    
    // 4. 设置 tabbar 标题颜色
    vc.tabBarItem.setTitleTextAttributes(
       [NSForegroundColorAttributeName:UIColor.orange],
       for: .highlighted)
    vc.tabBarItem.setTitleTextAttributes(
        [NSFontAttributeName:UIFont.systemFont(ofSize: 12)],
        for: .normal)
    
    
    // 实例化导航控制器的时候,会调用 push 方法,将 root 压栈
    let nav = LYWBNavigationController(rootViewController: vc)
    
    return nav
    
  }
  
}
